package cn.batch.db;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
	
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("cn/batch/db/applicationContext.xml");
		JobLauncher launcher = (JobLauncher) context.getBean("jobLauncher");
		
		Job job = (Job) context.getBean("dumpJob");
		String sql = "select * from cms_site";
		try {
        	Map<String, JobParameter> params = new HashMap<String, JobParameter>();
        	params.put("dump.sql", new JobParameter(sql));
        	params.put("dump.rowpojo", new JobParameter("cn.batch.db.Site"));
        	params.put("dump.filename", new JobParameter("site.csv"));
        	params.put("dump.fields", new JobParameter("id,name,title,logo,description"));
        	params.put("dump.time", new JobParameter((new Date()).getTime()));
        	
            JobExecution result = launcher.run(job, new JobParameters(params));
            System.out.println(result.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
		
		
		/*Job job = (Job) context.getBean("loaderJob");
		String sql = "insert into cms_site(id,name,title,logo,description) values (:id,:name,:title,:logo,:description)";
		
		try {
        	Map<String, JobParameter> params = new HashMap<String, JobParameter>();
        	params.put("input.file", new JobParameter("site.txt"));
        	params.put("input.tokens", new JobParameter("id,name,title,logo,description"));
        	params.put("input.rowpojo.type", new JobParameter("cn.batch.db.Site"));
        	params.put("input.sql", new JobParameter(sql));
        	params.put("dump.time", new JobParameter((new Date()).getTime()));
        	
            JobExecution result = launcher.run(job, new JobParameters(params));
            System.out.println(result.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }*/

	}

}
